package com.google.code.maven.plugin.http.client;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;


/**
 * 
 * @author 797409
 * 
 */
public class HttpRequestUtils {
	public static HttpEntity query(DefaultHttpClient httpclient, Request request, Proxy proxy, Log log) throws ClientProtocolException, IOException, MojoExecutionException {
		log.info("Executing " + request);
		if (proxy != null) {
			log.info("setting up " + proxy);
			proxy.prepare(httpclient);
		}
		HttpRequestBase httpRequest = request.buildHttpRequestBase(httpclient, log);
		HttpHost targetHost = request.buildHttpHost(log);

		long responseTime = System.currentTimeMillis();
		HttpResponse response = httpclient.execute(targetHost, httpRequest);
		HttpEntity entity = response.getEntity();
		log.info("received response (time=" + (System.currentTimeMillis() - responseTime) + "ms)");
		return entity;
	}
}
